import axios from "axios"
// import {createBrowserHistory} from "history"
import {Modal} from "antd"
import Loading from "@/components/Loading"
let http = axios.create({
    baseURL:"/api",
    withCredentials:true,
    timeout:5000
})

http.interceptors.request.use(config=>{
    // console.log('require',config);

    let token = JSON.parse(localStorage.getItem('qf-token')!)
    config.headers!.authorization = token!
    return config
})

// let history = createBrowserHistory()
http.interceptors.response.use(config=>{
    console.log('resopne',config);
    Loading({visible:false})
    if(config.data.code === "1004" || config.data.code === "10022"){ //token验证失败 和 登录过期
        Modal.error({
            title:'错误提示',
            content: config.data.msg,
            centered:true,
            onOk(){
                localStorage.removeItem('qf-token')
                // history.push('/login')  //第一种方式
                window.location.href= '/login'  //第二种方式
                // window.location.reload()  //第三种方式
            }   
        }) 

        
    } else if(!config.data.state){
        Modal.error({
            title:'错误提示',
            content: config.data.msg,
            centered:true
        })
    } else if(config.data.state){
        if(config.data.msg !== '获取成功'){
            Modal.success({
                content: config.data.msg,
                centered:true,
            })
        }
    }

    return config
})

export default http